<?php
function str_limit($str, $n = 300)
{
    if (strlen($str) <= $n)
    {
        return $str;
    }
    
	 $limit = strpos($str,' ', $n) ;
     while($limit==false&&$n>1)
     {    $n--;
         $limit = strpos($str,' ', $n) ;
     }
	 $limit = $limit ? $limit : $n ;
	 $out = substr($str, 0, $limit) ;
	 return $out.' ...' ;
}

function kodau($str)
{
	$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
	$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
	$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
	$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
	$str = preg_replace("/(đ)/", 'd', $str);
	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
	$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
	$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
	$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
	$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
	$str = preg_replace("/(Đ)/", 'D', $str);
	$str = preg_replace("/( )/", '-', $str);
	$str = preg_replace("/(\'|\"|`|&|,|\.|\?)/", '', $str);
	$str = preg_replace("/(---|--)/", '-', $str);
	
	$pattern = '/([^a-z0-9\-\._])/i';
	$str = preg_replace($pattern, '', $str);
		
	$str = strtolower($str);
	return $str;
}

function op_town()
{
	$ci = get_instance();
	$list = $ci->db->query('select * from town order by town_name')->result() ;
	$str = '<option value="0"> -- </option>' ;
	foreach ($list as $rs) :
		$str .= '<option value="'.$rs->town_id.'">'.$rs->town_name.'</option>' ;
	endforeach ;
	return $str ;
}
function ajax_paging($totalrow,$curpage,$pageto,$maxrow=5,$maxpage=3)
{
	$paging = "";
	if($totalrow % $maxrow == 0) {
		$totalpage = (int)($totalrow/$maxrow);
	} else {
		$totalpage = (int)($totalrow /$maxrow)+ 1;
	}
	if($totalrow > $maxrow ){
		$start = 1;
		$end   = 1;
		$navi  = "" ;
		
		//$list = "Page: ".$curpage ." in ".$totalpage ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		$list = "" ;
		for($i=0;$i<=$totalpage;$i++){
			if(($i > ((int)(($curpage - 1)/$maxpage))*$maxpage)&&($i <= ((int)(($curpage -1)/$maxpage + 1))*$maxpage)){
				if($start ==1 )$start = $i ;
				if($i == $curpage){
					$navi .= "<strong>".$i ."</strong>";
				}else {
					if($pageto=='javascript')
						$navi .= "<a href='javascript:gotopage(".$i.")'>$i</a>" ;
					else
						$navi .= "<a href='".$pageto."&page=$i'>$i</a>";
				}
				$end = $i ;
			}			
		}
		//khai báo preview 
		if($curpage > $maxpage){
			if($pageto=='javascript')
				$preview = "<a href='javascript:gotopage(".($start - 1).")'><span class=prev>&nbsp;</span></a>";
			else
				$preview = "<a href='".$pageto."&page=".($start - 1)."'><span class=prev>&nbsp;</span></a>";
		}else{
			$preview = '';//"<strong><<</strong>" ;
		}
		// khai báo next 
		$i = ((int)(($curpage -1)/$maxpage + 1))*$maxpage;
		if($i < $totalpage){
			if($pageto=='javascript')
				$next = "<a href='javascript:gotopage(".($end + 1).")'><span class=next>&nbsp;</span></a>";
			else
				$next = "<a href='".$pageto."&page=".($end + 1)."'><span class=next>&nbsp;</span></a>";
		} else {
			$next = '';//"<strong> >> </strong>" ;
		}
		$paging .= $list . $preview . $navi .$next ;
	}
	return $paging ;
}
function pricevn($price)
{
	return number_format($price, 0,',',".") ;
}
function get_id_from_alias($url)
{
	$str_id = end(explode('-',$url));
	$pos = strpos($str_id,'.html');
	if($pos===false){
		$id=0;
	}else{
		$id = (int)substr($str_id,0,$pos);
	}
	return $id;
}
function getIp() {
	return (getenv('HTTP_X_FORWARDED_FOR')) ?  getenv('HTTP_X_FORWARDED_FOR') :  getenv('REMOTE_ADDR');
}
function getUserFromEmail($email) {
	$arrEamil=explode('@',$email);
	return $arrEamil[0];
}

function rand_string($size){
   $string = "aAbBcCdDeEeFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVmWxXzYyZ";
   $rand_str = "";
   for ($i=0; $i<$size; $i++)
   {
	   $rand_str .= substr($string, rand(0, strlen($string)-1), 1);
   }
   return $rand_str;
}
function encrypt($str){
	for($i = 0; $i <2; $i ++){
		$str = base64_encode(base64_encode($str));
	}
	$str= rand_string(5).$str;
	return $str;	
}
function decrypt($str){
	$str=substr($str,5,strlen($str));		
	for($i = 0; $i < 2; $i ++){
		$str = base64_decode(base64_decode($str));
	}
	
	return $str;
}
?>
